package com.ny.service.impl;

import com.ny.mapper.EquipmentMaintenanceMapper;
import com.ny.mapper.EquipmentMapper;
import com.ny.domain.EquipmentDO;
import com.ny.dto.EquipmentDTO;
import com.ny.dto.EquipmentMaintenanceDTO;
import com.ny.query.EquipmentQuery;
import com.ny.service.EquipmentService;
import java.lang.Boolean;
import java.lang.Override;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ny.domain.EquipmentMaintenanceDO;
import com.ny.exception.BusinessException;

/**
 * 器材管理模块的实现
 *
 * @author MSI-NB
 * @date 2025-03-08 13:14:38
 */
@Service
public class EquipmentServiceImpl implements EquipmentService {

    @Autowired
    private EquipmentMaintenanceMapper equipmentMaintenanceMapper;

    @Autowired
    private EquipmentMapper equipmentMapper;

    @Override
    public Boolean addEquipment(EquipmentDTO equipmentDTO) {
        // 检查器材名称是否已存在
        if (equipmentMapper.selectByName(equipmentDTO.getEquipmentName()) != null) {
            throw new BusinessException("器材名称已存在，请更换其他名称。");
        }
        // 将器材信息保存到数据库
        EquipmentDO equipmentDO = new EquipmentDO();
        equipmentDO.setEquipmentName(equipmentDTO.getEquipmentName());
        equipmentDO.setEquipmentDescription(equipmentDTO.getEquipmentDescription());
        equipmentDO.setLocation(equipmentDTO.getLocation());
        equipmentDO.setStatus(equipmentDTO.getStatus());
        int result = equipmentMapper.insert(equipmentDO);
        return result > 0;
    }

    @Override
    public Boolean deleteEquipment(EquipmentDTO equipmentDTO) {
        // 检查器材信息是否存在
        EquipmentDO equipmentDO = equipmentMapper.selectById(equipmentDTO.getEquipmentId());
        if (equipmentDO == null) {
            throw new BusinessException("器材信息不存在，无法删除。");
        }
        // 删除器材信息
        int result = equipmentMapper.deleteById(equipmentDTO.getEquipmentId());
        return result > 0;
    }

    @Override
    public Boolean updateEquipment(EquipmentDTO equipmentDTO) {
        // 检查器材信息是否存在
        EquipmentDO equipmentDO = equipmentMapper.selectById(equipmentDTO.getEquipmentId());
        if (equipmentDO == null) {
            throw new BusinessException("器材信息不存在，无法更新。");
        }
        // 更新器材信息到数据库
        equipmentDO.setEquipmentName(equipmentDTO.getEquipmentName());
        equipmentDO.setEquipmentDescription(equipmentDTO.getEquipmentDescription());
        equipmentDO.setLocation(equipmentDTO.getLocation());
        equipmentDO.setStatus(equipmentDTO.getStatus());
        int result = equipmentMapper.updateById(equipmentDO);
        return result > 0;
    }

    @Override
    public EquipmentDO infoEquipment(EquipmentQuery equipmentQuery) {
        // 检查器材信息是否存在
        EquipmentDO equipmentDO = equipmentMapper.selectById(equipmentQuery.getEquipmentId());
        if (equipmentDO == null) {
            throw new BusinessException("器材信息不存在，无法查询。");
        }
        // 查询指定器材的详细信息
        return equipmentDO;
    }

    @Override
    public Boolean addMaintenance(EquipmentMaintenanceDTO equipmentMaintenanceDTO) {
        // 检查器材信息是否存在
        EquipmentDO equipmentDO = equipmentMapper.selectById(equipmentMaintenanceDTO.getEquipmentId());
        if (equipmentDO == null) {
            throw new BusinessException("器材信息不存在，无法添加维护记录。");
        }
        // 将维护记录保存到数据库
        EquipmentMaintenanceDO equipmentMaintenanceDO = new EquipmentMaintenanceDO();
        equipmentMaintenanceDO.setEquipmentId(equipmentMaintenanceDTO.getEquipmentId());
        equipmentMaintenanceDO.setMaintenanceDescription(equipmentMaintenanceDTO.getMaintenanceDescription());
        equipmentMaintenanceDO.setMaintenanceDate(equipmentMaintenanceDTO.getMaintenanceDate());
        int result = equipmentMaintenanceMapper.insert(equipmentMaintenanceDO);
        return result > 0;
    }

    @Override
    public Boolean deleteMaintenance(EquipmentMaintenanceDTO equipmentMaintenanceDTO) {
        // 检查维护记录是否存在
        EquipmentMaintenanceDO equipmentMaintenanceDO = equipmentMaintenanceMapper.selectById(equipmentMaintenanceDTO.getMaintenanceId());
        if (equipmentMaintenanceDO == null) {
            throw new BusinessException("维护记录不存在，无法删除。");
        }
        // 删除指定的维护记录
        int result = equipmentMaintenanceMapper.deleteById(equipmentMaintenanceDTO.getMaintenanceId());
        return result > 0;
    }

    @Override
    public Boolean updateMaintenance(EquipmentMaintenanceDTO equipmentMaintenanceDTO) {
        // 检查维护记录是否存在
        EquipmentMaintenanceDO equipmentMaintenanceDO = equipmentMaintenanceMapper.selectById(equipmentMaintenanceDTO.getMaintenanceId());
        if (equipmentMaintenanceDO == null) {
            throw new BusinessException("维护记录不存在，无法更新。");
        }
        // 更新维护记录到数据库
        equipmentMaintenanceDO.setEquipmentId(equipmentMaintenanceDTO.getEquipmentId());
        equipmentMaintenanceDO.setMaintenanceDescription(equipmentMaintenanceDTO.getMaintenanceDescription());
        equipmentMaintenanceDO.setMaintenanceDate(equipmentMaintenanceDTO.getMaintenanceDate());
        int result = equipmentMaintenanceMapper.updateById(equipmentMaintenanceDO);
        return result > 0;
    }

    @Override
    public List<EquipmentDO> findPageList(int pageNum, int pageSize) {
        List<EquipmentDO> equipmentDOS = equipmentMapper.selectAll();

        if (CollectionUtils.isEmpty(equipmentDOS)){
            EquipmentDO equipmentDO = new EquipmentDO();
            equipmentDO.setEquipmentId(1);
            equipmentDO.setEquipmentName("哑铃");
            equipmentDO.setEquipmentDescription("锻炼肌肉力量的器材");
            equipmentDO.setLocation("A2健身区");
            equipmentDO.setStatus("正常");
            equipmentDOS.add(equipmentDO);
        }

        return equipmentDOS;
    }
}
